package com.sgb.kjwl.viewmodel.machine;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.repository.DeviceRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.self.MyPriceListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@Deprecated
public class PublicMyPriceModel extends BaseViewModel {

    private int page = 1;
    private int size = 10;
    public boolean hasNext = false;//是否有下一页


    private MyPriceListFragment myPriceListFragment;
    private DeviceRepertory repertory;

    public PublicMyPriceModel(MyPriceListFragment myPriceListFragment) {
        this.myPriceListFragment = myPriceListFragment;
    }

    //获取租赁经营———我的报价--列表数据(获取列表首页)
    public void getDataList(String isAcceptQuote, boolean isLoadMore) {
//        page = isLoadMore ? page + 1 : 1;
//        APIManager.getInstance().getLeaseAPI().getPackersFinanceList(page, size, isAcceptQuote).enqueue(new Callback<BaseEntity<PublicMyPriceEntity>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<PublicMyPriceEntity>> call, Response<BaseEntity<PublicMyPriceEntity>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (isLoadMore) {
//                        myPriceListFragment.showMoreList(response.body().getData().getDataList());
//                    } else {
//                        myPriceListFragment.showList(response.body().getData().getDataList());
//                    }
//                    if (page < response.body().getData().getTotalPage()) {
//                        hasNext = true;
//                    } else {
//                        hasNext = false;
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<PublicMyPriceEntity>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
    }

//    //更改报价
//
//    public void changePrice(String id, String price, String remark, String quoteUtil, String categoryType) {
//        DialogHelper.showProgressDialog(myPriceListFragment.getContext(), null, "更改报价中...", 0, false, null).setCanceledOnTouchOutside(false);
//        page++;
//        Map map = new HashMap();
//        map.put("rentalSeekId", id);
//        map.put("quoteUtil", quoteUtil);
////        map.put("quoteKey", quoteKey);
//        map.put("quotePrice", price);
//        map.put("remark", remark);
//        map.put("categoryType", categoryType);//机械，1设备 2车辆，3房产，4土地 5工厂 6农机
//        APIManager.getInstance().getMachineAPI().changMyQuote(map).enqueue(new Callback<BaseEntity>() {
//            @Override
//            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    //更改报价成功
//                    myPriceListFragment.showDelSuccToast("更改报价成功...");
//
//                    myPriceListFragment.initData(false);//更改成功刷新页面
//
//                } else {
//                    MToast.showToast(myPriceListFragment.getActivity(), response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//                ToastUtils.show(myPriceListFragment.getContext(), "更改报价失败");
//            }
//        });
//    }

    //删除报价
    public void delPrice(String id) {
        DialogHelper.showProgressDialog(myPriceListFragment.getContext(), null, "删除报价中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().deleteMyQuote(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    myPriceListFragment.showDelSuccToast("删除报价成功...");
                    myPriceListFragment.loadData(false);//删除成功刷新页面
                } else {
                    MToast.showToast(myPriceListFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(myPriceListFragment.getContext(), "删除报价失败");
            }
        });

    }

    public void getH5Url(String id, String categoryType) {
        String name = "";
        if ("4".equals(categoryType)) {//土地
            name = "landOffer";
        } else if ("5".equals(categoryType)) {
            name = "factoryOffer";//工厂
        } else if ("6".equals(categoryType)) {
            name = "farmOffer";//农机
        } else {
            name = "myOffers";
        }
        DialogHelper.showProgressDialog(myPriceListFragment.getContext(), "加载中...", 0, false).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&categoryType=" + categoryType;
//                    PublicMyPriceDetailsH5Activity.start(myPriceListFragment.getContext(),url);
//                    WebViewX5HostActivity.start(myPriceListFragment.getContext(),url);
//                    myPriceListFragment.goH5(url);
                    WebViewX5HostActivity.start(myPriceListFragment.getContext(), url);
                } else {
                    MToast.showToast(myPriceListFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
